package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ACustomer;
import com.ruoyi.system.service.IACustomerService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员管理Controller
 *
 * @author 王家宁
 * @date 2024-08-18
 */
@Controller
@RequestMapping("/system/customer")
public class ACustomerController extends BaseController {
    private String prefix = "system/customer";

    @Autowired
    private IACustomerService aCustomerService;

    @Autowired
    private ISysUserService userService;

    @GetMapping()
    public String customer() {
        return prefix + "/customer";
    }

    /**
     * 查询会员管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ACustomer aCustomer) {
        startPage();
        List<ACustomer> list = aCustomerService.selectACustomerList(aCustomer);
        return getDataTable(list);
    }

    /**
     * 导出会员管理列表
     */
    @RequiresPermissions("system:customer:export")
    @Log(title = "会员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ACustomer aCustomer) {
        List<ACustomer> list = aCustomerService.selectACustomerList(aCustomer);
        ExcelUtil<ACustomer> util = new ExcelUtil<ACustomer>(ACustomer.class);
        return util.exportExcel(list, "会员管理数据");
    }

    /**
     * 新增会员管理
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        List<SysUser> sysUsers = userService.selectUserList(new SysUser());
        modelMap.put("users", sysUsers);
        return prefix + "/add";
    }

    /**
     * 新增保存会员管理
     */
    @RequiresPermissions("system:customer:add")
    @Log(title = "会员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ACustomer aCustomer) {
        return toAjax(aCustomerService.insertACustomer(aCustomer));
    }

    /**
     * 修改会员管理
     */
    @RequiresPermissions("system:customer:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ACustomer aCustomer = aCustomerService.selectACustomerById(id);
        mmap.put("aCustomer", aCustomer);
        List<SysUser> sysUsers = userService.selectUserList(new SysUser());
        mmap.put("users", sysUsers);
        return prefix + "/edit";
    }

    /**
     * 修改保存会员管理
     */
    @RequiresPermissions("system:customer:edit")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ACustomer aCustomer) {
        return toAjax(aCustomerService.updateACustomer(aCustomer));
    }

    /**
     * 删除会员管理
     */
    @RequiresPermissions("system:customer:remove")
    @Log(title = "会员管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(aCustomerService.deleteACustomerByIds(ids));
    }
}
